/**
 * Ecart
 * 
 * This file is part of Ecart.
 * 
 * Ecart is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Ecart is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Ecart.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright   Copyright 2008-2009 E-Cart LLC
 * @license     GNU Public License V3.0
 */

Ext.onReady(function(){
    
    var htmlEditor = new Ext.form.HtmlEditor({
        name: 'content',
        fieldLabel: 'Content'.l(),
        anchor: '99%'
    });
    
     commentForm = new Ext.form.FormPanel({
          labelWidth: 80,
        autoScroll: true,
        border: false,
        labelAlign: 'top',
        bodyStyle: 'padding: 5px 5px 0px 5px',
        reader: new Ext.data.JsonReader({
            root: 'comment'
            },
            ['author', 'email', 'status', 'content']
        ),
        items: [{
            layout: 'column',
            border: false,
            bodyStyle: 'padding: 5px 0px 0px',
            items: [{
                columnWidth: .3,
                layout: 'form',
                border: false,
                items: [{
                    xtype: 'textfield',
                    fieldLabel: 'Author'.l(),
                    name: 'author',
                    allowBlank: false,
                    maxLength: 45,
                    anchor: '95%'
                }]
            }, {
                columnWidth: .4,
                layout: 'form',
                border: false,
                items: [{
                    fieldLabel: 'Email'.l(),
                    xtype: 'textfield',
                    name: 'email',
                         vtype: 'email',
                    anchor: '95%',
                    maxLength: 45
                }]
            }, {
                columnWidth: .3,
                layout: 'form',
                border: false,
                items: [{
                    fieldLabel: 'Status'.l(),
                    xtype: 'combo',
                    name: 'status',
                    emptyText: 'Select status'.l(),
                    hiddenName: 'status',
                    store: new Ext.data.SimpleStore({
                        fields: ['id', 'value'],
                        data: status
                    }),
                    displayField: 'value',
                    valueField: 'id',
                    mode: 'local',
                    editable: false,
                    triggerAction: 'all',
                    anchor: '95%',
                    maxLength: 45
                }]
            }]
        }, htmlEditor]
     });
     
     commentWindow = new Ext.Window({
        closeAction: 'hide',
          plain: false,
        width: 640,
        height: 490,
        maximizable: true,
        layout: 'fit',
        title: 'Comment',
          items: commentForm,
        buttons: 
        [{
            text: 'Save'.l(),
               handler: submitComment
        },
        {
            text: 'Cancel'.l(),
            handler: function(){
                commentWindow.hide();
            }           
        }]
     })
     
     commentForm.on('resize', function(){
        commentWindow.doLayout();
    });
     
     /*
     commentForm.findById('htmlEditor').on('render', function() {
    
         var imageButton = new Ext.Toolbar.Button ({
             icon: Ecart.skinUrl + '/images/icons/image.png',
             id: 'imageButton',
             cls: 'x-btn-icon',
             tooltip: {text: 'Insert image into comment', title: 'Insert image'},
               handler: function(){
                    imageWindow.show();
               } 
         });
          
        var tb = commentForm.findById('htmlEditor').getToolbar();
          
          tb.addSeparator();
        tb.add(imageButton);
          
     })
     */
     
})

function submitComment() {
     commentForm.getForm().submit({
          url: Ecart.getUrl('cms_comment/save-comment'),
          params: {commentId: comment, pageId: page},
        success: function(){
            commentWindow.hide();
            commentGrid.getStore().reload();
        }
     })
}
